package com.lyc.reflect;

import java.lang.reflect.Field;

/**
 * @author ：Xiaochen
 * @date ：Created in 2022/10/27 9:54
 * @description：
 * @modified By：
 * @version:
 */
public class Text02 {
    public static void main(String[] args) throws Exception {
        Class<Teacher> teacherClass = Teacher.class;
//        Field name = teacherClass.getDeclaredField("name");
//        System.out.println(name);
//        Field age = teacherClass.getDeclaredField("age");
//        System.out.println(age);
//
//
//        Field[] declaredFields = teacherClass.getDeclaredFields();
//        for (Field field:declaredFields){
//            System.out.println(field);
//        }
//
//        Field name1 = teacherClass.getField("name");
//        System.out.println(name1);
//
//        Field address = teacherClass.getField("address");
//        System.out.println(address);

        Teacher teacher = teacherClass.newInstance();
        System.out.println(teacher);

        Field name = teacherClass.getDeclaredField("name");
        name.set(teacher,"小诗诗");
        System.out.println(teacher);
        Object o = name.get(teacher);
        System.out.println(o);

        Field age = teacherClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(teacher,18);
        System.out.println(teacher);
        Object o1 = age.get(teacher);
        System.out.println(o1);


    }
}

class People{
    public String address;
    private String sex;
}

class Teacher extends People{
    public String name;
    private Integer age;

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}